package eai.tp2;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import eai.tp2.core.CameraListReader;
import eai.tp2.core.CameraSummaryWriter;
import eai.tp2.core.SummaryEngine;
import eai.tp2.core.datamodel.CameraSummary;

public class Wrapper {
	
	 public static void main(String[] args) {
		 System.setSecurityManager(null);
		 if(args.length != 1)
		 {
			 System.out.println("ERROR: ARGS != 1");			 
			 return;
		 }		 
		 else {
			 try {
				 	SAXBuilder builder = new SAXBuilder(false);
				 	Document docIn = builder.build(args[0]);				 	
	                SummaryEngine engine = new SummaryEngine();
	                List<Element> elements = (List<Element>)docIn.getRootElement().getChildren();
	                HashMap<String, List<Element>> map = new HashMap<String, List<Element>>();
	                map.put("unknown", elements);
	                List<CameraSummary> cameras = engine.process(map);
	                CameraSummaryWriter writer = new CameraSummaryWriter(cameras);	                
	                Document docOut = writer.write(cameras.get(0));
	                XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
					System.out.println(outputter.outputString(docOut));
	            } catch (JDOMException ex) {
	                System.out.println("[ERROR] XML file not valid. Please notice if the camera brand has models."+ex.getLocalizedMessage());
	            } catch (IOException ex) {
	                System.out.println("[ERROR] Please type the right camera XML file, or confirm that the XML file exist.");               
	            } 
		 }
	        
	 }	
}
